package com.ocn.li.scmdy.modules.app.controller;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ocn.li.scmdy.common.utils.R;
import com.ocn.li.scmdy.modules.app.annotation.Login;
import com.ocn.li.scmdy.modules.app.entity.ChannelEntity;
import com.ocn.li.scmdy.modules.app.entity.CoinDetailEntity;
import com.ocn.li.scmdy.modules.app.entity.UserEntity;
import com.ocn.li.scmdy.modules.app.form.RequestForm;
import com.ocn.li.scmdy.modules.app.service.ChannelService;
import com.ocn.li.scmdy.modules.app.service.CoinDetailService;
import com.ocn.li.scmdy.modules.app.service.UserService;
import com.ocn.li.scmdy.modules.app.utils.RsaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * TODO 会员相关操作接口
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/11
 */
@RestController
@RequestMapping("/app/user")
@Slf4j
public class AppUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private CoinDetailService coinDetailService;

    /**
     * 获取用户详情
     *
     * @param userId
     * @return
     */
    @Login
    @PostMapping("/info")
    public R findUserinfoByUserId(@RequestAttribute("userId") Long userId){

        UserEntity user = userService.selectById(userId);
        return R.ok().put("data",user);
    }

    @Login
    @PostMapping("/coin")
    public R coin(@RequestAttribute("userId") Long userId, @RequestAttribute("channelCode") String code, @RequestBody RequestForm form){

        UserEntity user = userService.selectById(userId);
        ChannelEntity channel = channelService.selectOne(new EntityWrapper<ChannelEntity>()
                .eq(code != null,"code", code));

        String privateKey = channel.getPrivateKey();

        // 获取到明文
        String jsonString = RsaUtils.decode(form.getEncrypt(),privateKey);

        // 操作积分明细
        CoinDetailEntity coin = JSON.parseObject(jsonString,CoinDetailEntity.class);

        coin.setCreateTime(new Date());
        coin.setChannelCode(code);
        coin.setChannelName(channel.getName());
        coin.setMobile(user.getMobile());
        coin.setSerialNumber(RandomUtil.randomNumbers(16));

        coinDetailService.insert(coin);

        // 修改用户积分
        user.setCoin(user.getCoin()+coin.getCoin());
        userService.updateById(user);

        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("coin",user.getCoin());

        return R.ok().put("data",map);
    }

    @Login
    @PostMapping("/coinDetail")
    public R coinDetail(@ApiIgnore @RequestAttribute("userId") Long userId){

        UserEntity user = userService.selectById(userId);

        Map<String,String> map  = new HashMap<>();
        map.put("mobile",user.getMobile());
        List list = coinDetailService.selectPageByConditions(map);

        return R.ok().put("list",list);
    }

    @Login
    @PostMapping("/coinCount")
    public R coinCount(@ApiIgnore @RequestAttribute("userId") Long userId){
        UserEntity user = userService.selectById(userId);
        Long coin = user.getCoin();
        return R.ok().put("coin",coin);
    }

}
